/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import de.centigrade.bizlaf.BizLafControl;
import de.centigrade.bizlaf.support.BizLafStyles;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import net.atlanticbb.tantlinger.shef.HTMLEditorPane;
import org.jsoup.Jsoup;

/**
 *
 * @author pato
 */
public final class detalleproducto extends javax.swing.JDialog {

    /**
     * Creates new form detalleproducto
     */
    Connection cn;
    PreparedStatement ps;
    ResultSet rs;
    int code;
    Vector ext = new Vector(5,1);
    Vector img = new Vector(5,1);
    Vector imgId = new Vector(5,1);
    HTMLEditorPane editor = new HTMLEditorPane();

            JLabel imageProduct;
    public detalleproducto(java.awt.Frame parent, boolean modal, int cd) {
        super(parent, modal);
        initComponents();
        BizLafControl.get().setStyle(jPanel1, BizLafStyles.CONTAINER);
        BizLafControl.get().setStyle(descripcion, BizLafStyles.NOTEPAD);
        BizLafControl.get().setStyle(notas, BizLafStyles.NOTEPAD);
        BizLafControl.get().setStyle(jScrollPane1, BizLafStyles.NOTEPAD);
        BizLafControl.get().setStyle(jScrollPane3, BizLafStyles.NOTEPAD);
        BizLafControl.get().setStyle(panelImagen, BizLafStyles.SHOWCASE);
        BizLafControl.get().setStyle(imageCurrent, BizLafStyles.BEVELED_BOTTOM);
        BizLafControl.get().setStyle(imageMax, BizLafStyles.BEVELED_BOTTOM); 
        editor.remove(0);
        ((JTabbedPane)editor.getComponent(0)).removeTabAt(1);
      //  BizLafControl.get().setStyle(buttonright, BizLafStyles.PLAIN);
      //  BizLafControl.get().setStyle(buttonleft, BizLafStyles.PLAIN);
        code = cd;
        cargarDatos();
        /*  new HTMLFontColorAction();
         new HTMLLinkAction();
         new HTMLImageAction();*/
        especificacionesPanel.add(((JTabbedPane)editor.getComponent(0)).getComponent(2));
        especificacionesPanel.updateUI();
       // cargarDatos();
    }
     
    void cargarDatos() {
        String especificaciones = "";
        try {
            ps = (PreparedStatement) cache_connection.cn.prepareStatement("Select nombre,peso,descripcion,especificaciones,tipo,notas from productos where id_producto = " + code);
            rs = ps.executeQuery();
            rs.next();
            especificaciones = (rs.getObject("especificaciones") == null) ? "" : rs.getString("especificaciones");
            /*if (especificaciones.indexOf("&amp;") != -1) {
                especificaciones = especificaciones.replaceAll("&amp;", "&");// vervangt &amp; door &
            }
            if (especificaciones.indexOf("&quot;") != -1) {
                especificaciones = especificaciones.replaceAll("&quot;", "aa"); //vervangt &quot; door "
            }
            if (especificaciones.indexOf("&lt;") != -1) {
                especificaciones = especificaciones.replaceAll("&lt;", "<");
            }
            if (especificaciones.indexOf("&gt;") != -1) {
                especificaciones = especificaciones.replaceAll("&gt;", ">");
            }*/
            especificaciones =Jsoup.parse(especificaciones).text();
            idproducto.setText(String.valueOf(code));
            nombre.setText(rs.getString("nombre"));
            peso.setText(rs.getString("peso"));
            descripcion.setText(rs.getString("descripcion"));
            editor.setText(especificaciones);
            tipo.setText(rs.getString("tipo"));
            notas.setText(rs.getString("notas"));
            new cargarImagenes().start();
            this.setTitle(code+"-"+nombre.getText());
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        tipo = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        especificacionesPanel = new javax.swing.JPanel();
        idproducto = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        imageMax = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();
        imageCurrent = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        buttonright = new javax.swing.JButton();
        peso = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        buttonleft = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descripcion = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        notas = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        panelImagen = new javax.swing.JPanel();

        jLabel6.setText("jLabel6");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tipo.setText("-------");

        jLabel7.setText("Especificaciones:");

        especificacionesPanel.setLayout(new java.awt.GridLayout(1, 0));

        idproducto.setText("-------");

        jLabel1.setText("Codigo:");

        jLabel8.setText("Notas:");

        imageMax.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        imageMax.setText("0");

        jLabel9.setText(" / ");

        nombre.setText("-------");

        imageCurrent.setText("0");

        jLabel2.setText("Nombre:");

        buttonright.setText(">>");
        buttonright.setEnabled(false);
        buttonright.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonrightActionPerformed(evt);
            }
        });

        peso.setText("-------");

        jLabel5.setText("Tipo:");

        buttonleft.setText("<<");
        buttonleft.setEnabled(false);
        buttonleft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonleftActionPerformed(evt);
            }
        });

        jLabel4.setText("Peso:");

        descripcion.setEditable(false);
        descripcion.setColumns(20);
        descripcion.setRows(5);
        jScrollPane1.setViewportView(descripcion);

        notas.setEditable(false);
        notas.setColumns(20);
        notas.setRows(5);
        jScrollPane3.setViewportView(notas);

        jLabel3.setText("Descripcion:");

        panelImagen.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombre)
                            .addComponent(idproducto)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(peso))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(tipo))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel7))
                    .addComponent(especificacionesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 542, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane3)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(buttonleft, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                        .addComponent(imageCurrent, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imageMax, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(67, 67, 67)
                        .addComponent(buttonright, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel3))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(panelImagen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(idproducto))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(nombre))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(peso))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(tipo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(especificacionesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(panelImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(imageCurrent)
                            .addComponent(imageMax)
                            .addComponent(jLabel9)
                            .addComponent(buttonleft)
                            .addComponent(buttonright))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addGap(7, 7, 7)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
public class cargarImagenes extends Thread {
    @Override
    public void run() {
        try {
            ps =   cache_connection.cn.prepareStatement("SELECT * FROM archivos a,archivo_relacion b WHERE a.id = b.archivoId AND b.productoId = "+code);
            rs = ps.executeQuery();
            if(!rs.next()){   
                imageProduct = new JLabel("No existe imagen en la BD", JLabel.CENTER);
                imageProduct.setText("No existe imagen en la BD");
            } else {
                int imgMax = Integer.parseInt(imageMax.getText());
                imageCurrent.setText("1");
                do{
                    // Sumamos +1 al numero de imagenes por cada iteracion
                    imgMax+=1;
                    imageMax.setText(String.valueOf(imgMax));
                    ext.addElement(rs.getString("ext"));
                    img.addElement(ConvertirImagen(rs.getBytes("archivo"),(String)ext.get(0)));
                    imgId.addElement(rs.getInt("id"));
                    ImageIcon image=new ImageIcon(((Image)img.get(0)).getScaledInstance(200, 148, 10));
                    imageProduct = new JLabel("", image, JLabel.CENTER);
                } while (rs.next()); // Cicla mientras sigan habiendo lineas en el result set
                if(imgMax > 0){
                    buttonleft.setEnabled(true);
                    buttonright.setEnabled(true);
                }
            } 
            panelImagen.add( imageProduct, BorderLayout.CENTER );
        } catch (SQLException | IOException ex) {
            Logger.getLogger(detalleproducto.class.getName()).log(Level.SEVERE, null, ex);
        }
}
    private Image ConvertirImagen(byte[] bytes,String ext) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        Iterator readers = ImageIO.getImageReadersByFormatName(ext);
        ImageReader reader = (ImageReader) readers.next();
        Object source = bis; // File or InputStream
        ImageInputStream iis = ImageIO.createImageInputStream(source);
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        return reader.read(0, param);
    } 

}
    private void buttonleftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonleftActionPerformed
        int current=Integer.parseInt(imageCurrent.getText());
        int max=Integer.parseInt(imageMax.getText());
        if (current==1){
            current=max;
        } else current-=1;
        imageCurrent.setText(String.valueOf(current));
        imageProduct.setIcon(new ImageIcon(((Image)img.get(current-1)).getScaledInstance(200, 148, 10)));
    }//GEN-LAST:event_buttonleftActionPerformed

    private void buttonrightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonrightActionPerformed
        int current=Integer.parseInt(imageCurrent.getText());
        int max=Integer.parseInt(imageMax.getText());
        if (current==max){
            current=1;
        } else current+=1;
        imageCurrent.setText(String.valueOf(current));
        imageProduct.setIcon(new ImageIcon(((Image)img.get(current-1)).getScaledInstance(200, 148, 10)));
    }//GEN-LAST:event_buttonrightActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonleft;
    private javax.swing.JButton buttonright;
    private javax.swing.JTextArea descripcion;
    private javax.swing.JPanel especificacionesPanel;
    private javax.swing.JLabel idproducto;
    private javax.swing.JLabel imageCurrent;
    private javax.swing.JLabel imageMax;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel nombre;
    private javax.swing.JTextArea notas;
    private javax.swing.JPanel panelImagen;
    private javax.swing.JLabel peso;
    private javax.swing.JLabel tipo;
    // End of variables declaration//GEN-END:variables
}
